package org.schedule.mapper;

import org.apache.ibatis.annotations.*;
import org.schedule.entity.Schedule;

import java.util.List;

@Mapper
public interface ScheduleMapper {


    //新增日程
    // Insert a new schedule into the database
    @Insert("INSERT INTO schedule (userid, content, scheduledate, remindtime) " +
            "VALUES (#{userid}, #{content}, #{scheduledate}, #{remindtime})")
    int insertSchedule(Schedule schedule);

    // 修改日程
    @Update("UPDATE schedule SET userid = #{userid}, content = #{content}, scheduledate = #{scheduledate}, " +
            "remindtime = #{remindtime} WHERE id = #{id}")
    int updateSchedule(Schedule schedule);

    // 根据id删除日程
    @Delete("DELETE FROM schedule WHERE id = #{id}")
    int deleteSchedule(@Param("id") Integer id);

    // 根据id查询日程
    @Select("SELECT s.*, u.username FROM schedule s " +
            "JOIN user u ON s.userid = u.id WHERE s.id = #{id}")
    Schedule getScheduleById(@Param("id") Integer id);

    // 查询当前用户全部日程
    @Select("SELECT s.*, u.username FROM schedule s " +
            "JOIN user u ON s.userid = u.id")
    List<Schedule> getAllSchedules();

    @Select("SELECT s.*, u.username FROM schedule s " +
            "JOIN user u ON s.userid = u.id WHERE s.userid = #{userId}")
    List<Schedule> getSchedulesByUserId(@Param("userId") Integer userId);

    @Select("SELECT s.*, u.username FROM schedule s " +
            "JOIN user u ON s.userid = u.id WHERE s.userid = #{userId} and s.content LIKE CONCAT('%', #{content}, '%')")
    List<Schedule> searchSchedulesByContent(@Param("userId") Integer userid, @Param("content") String content);
}
